package com.example.administrator.newsapp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.example.administrator.newsapp.pojo.Comment;
import com.example.administrator.newsapp.pojo.Favorite;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

public class ContentActivity extends AppCompatActivity {

    private static final int LOGIN_SUC = 102;

    private WebView mWebView;
    private Toolbar toolbar;
    private Button btnFa;
    private EditText etComment;
    private BmobUser bmobUser = BmobUser.getCurrentUser();
    private ListView mListView;
    private ListCommentAdapter listCommentAdapter;
    private List<Comment> mCommentList = new ArrayList<>();
    private String newId;
    private String new_url;
    private String new_image;
    private String new_title;
    String favObjectId;
    int zanNum;
    boolean isFav=false;//是否收藏过
    boolean isOne = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content);
        initView();
        Intent intent = getIntent();
        new_url = intent.getStringExtra("URL");
        new_image=intent.getStringExtra("IMAGE");
        new_title=intent.getStringExtra("TITLE");
        newId = intent.getStringExtra("NEWID");
        mWebView.loadUrl(new_url);

        toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        //评论功能
        btnFa.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(bmobUser==null){
                    Toast.makeText(ContentActivity.this,"请先登录",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(!etComment.getText().equals("")){
                    String comment = etComment.getText().toString();
                    Comment c = new Comment();
                    c.setUser_id(bmobUser.getObjectId());
                    c.setNews_id(newId);
                    c.setComment_content(comment);
                    c.save(new SaveListener<String>() {
                        @Override
                        public void done(String s, BmobException e) {
                            if(e==null){
                                Toast.makeText(ContentActivity.this,"发表成功",Toast.LENGTH_SHORT).show();
                                etComment.setText("");
                                lookComment();
                            }
                            else{
                                Toast.makeText(ContentActivity.this,"发表失败",Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
            }
        });
        //查看评论
        lookComment();
    }

    private void lookComment() {
        BmobQuery<Comment> query = new BmobQuery<>();
        query.addWhereEqualTo("news_id", newId);
        query.findObjects(new FindListener<Comment>() {
            @Override
            public void done(List<Comment> list, BmobException e) {
                Log.e("BmobQuery", "查询成功：共"+list.size()+"条评论。");
                mCommentList=list;
//                Log.e("BmobQuery", mCommentList.size()+"");
                listCommentAdapter = new ListCommentAdapter(mCommentList,ContentActivity.this);
                mListView.setAdapter(listCommentAdapter);
                listCommentAdapter.setOnItemZanClickListener(new ListCommentAdapter.onItemZanListener() {
                    @Override
                    public void onZanClick(int i) {
                        zanNum=mCommentList.get(i).getZanNum();
                        String objectId = mCommentList.get(i).getObjectId();
                        zanNum++;
                        updateZan(objectId);
//                        Log.e("ZAN",zanNum+""  );
                        lookComment();
                    }
                });
            }
        });
    }

    //后台更新赞的数
    private void updateZan(String objectId) {
        Comment comment = new Comment();
        comment.setZanNum(zanNum);
        comment.update(objectId, new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if(e==null){
                    Log.e("ZANHUOTAI","后台zan修改成功"  );
                }
            }
        });
    }

    private void initView() {
        mWebView = findViewById(R.id.web_view);
        btnFa = findViewById(R.id.btn_fa);
        etComment = findViewById(R.id.et_comment);
        mListView = findViewById(R.id.list_comment);
    }

    //初始化Toolbar按钮
    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar,menu);
        if(isOne){
        //查看已收藏的数据
        BmobQuery<Favorite> query = new BmobQuery<>();
        query.findObjects(new FindListener<Favorite>() {
            @Override
            public void done(List<Favorite> list, BmobException e) {
                if (e == null) {
                    for (int i = 0; i < list.size(); i++) {
                        Favorite favorite = list.get(i);
                        if (favorite.getNews_id().equals(newId) && favorite.isIs_fav()) {
                            MenuItem menuItem = menu.findItem(R.id.fav);
                            menuItem.setIcon(R.drawable.ic_choose_fav);
                            menuItem.setTitle("取消收藏");
                            favObjectId = favorite.getObjectId();
                            isFav = true;
                            isOne=false;
                        }
                    }
                }
            }
        });
        }
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        MenuItem menuItem = menu.findItem(R.id.fav);
        if(isFav){
            menuItem.setIcon(R.drawable.ic_choose_fav);
            menuItem.setTitle("取消收藏");
            //添加收藏数据进后台
            Favorite f = new Favorite();
            f.setNews_id(newId);
            f.setUser_id(bmobUser.getObjectId());
            f.setIs_fav(isFav);
            f.setNew_image(new_image);
            f.setNew_title(new_title);
            f.setNew_url(new_url);
            f.save(new SaveListener<String>() {
                @Override
                public void done(String s, BmobException e) {
                    if(e==null){
                        Toast.makeText(ContentActivity.this,"收藏成功",Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }else {
            if(favObjectId!=""){
                menuItem.setIcon(R.drawable.ic_fav);
                menuItem.setTitle("收藏");
                Favorite f = new Favorite();
                f.delete(favObjectId, new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if(e==null){
                            Toast.makeText(ContentActivity.this,"取消了收藏",Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        }
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                break;
            case R.id.fav:
                //不收藏，要收藏
                if(!isFav){
                    isFav=true;
                    invalidateOptionsMenu();
                }else {
                    isFav=false;
                    invalidateOptionsMenu();
                }
                break;
        }
        return true;
    }
}
